# coding:utf8
# print (dir())

# for in 循环原理  可迭代对象通过iter方法获取迭代器对象，然后通过调用next()方法进行遍历
l = [1, 2, 3]
print(dir(l))
print(iter(l))

t = iter(l)
print(dir(t))
print(t.next())
print(t.next())
print(t.next())

import requests


def getData(pageNum):
    r = requests.get('http://www.shisongyan.top/msg/list?pageSize=5&pageNum=' + pageNum)
    msg = r.json()['result.txt']['list']
    for i in msg:
        print (i['message'])
    print("-" * 50)


from collections import Iterable, Iterator


class getDataIterator(Iterator):
    def __init__(self, pageNums):
        self.pageNums = pageNums
        self.index = 0

    def getData(self, pageNum):
        r = requests.get('http://www.shisongyan.top/msg/list?pageSize=5&pageNum=' + pageNum)
        print r
        msg = r.json()['result.txt']['list']
        return msg

    def next(self):
        if self.index == len(self.pageNums):
            raise StopIteration
        pageNum = self.pageNums[self.index]
        self.index += 1
        return self.getData(pageNum)

class getDataIterable(Iterable):
    def __init__(self, pageNums):
        self.pageNums = pageNums

    def __iter__(self):
        return getDataIterator(self.pageNums)

for x in getDataIterable(['1','2','3']):
    for i in x:
        print(i['message'])